/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#include <atomic>
#include <kmhdfs/client/file_system_impl.h>
#include <memory>
#include <kmhdfs/client/output_stream.h>
#include <kmhdfs/client/output_stream_impl.h>
#include <kmhdfs/client/striped_output_stream_impl.h>

using namespace Hdfs::Internal;

namespace Hdfs {

OutputStream::OutputStream() {
    impl = new Internal::OutputStreamImpl;
}

OutputStream::OutputStream(std::shared_ptr<ECPolicy> ecPolicy) {
    impl = new Internal::StripedOutputStreamImpl(ecPolicy);
}

OutputStream::~OutputStream() {
    delete impl;
}

void OutputStream::open(FileSystem & fs, const char * path,
                        std::pair<std::shared_ptr<LocatedBlock>, std::shared_ptr<Hdfs::FileStatus>> & pair,
                        int flag, const Permission permission, bool createParent, int replication,
                        int64_t blockSize, int64_t fileId) {
    if (!fs.impl) {
        THROW(HdfsIOException, "FileSystem: not connected.");
    }

    impl->open(fs.impl->filesystem, path, pair, flag, permission, createParent, replication,
               blockSize, fileId);
}

void OutputStream::open(FileSystem & fs, const char * path,
                        int flag, const Permission permission, bool createParent, int replication,
                        int64_t blockSize) {
    std::pair<std::shared_ptr<LocatedBlock>, std::shared_ptr<Hdfs::FileStatus>> pair;
    open(fs, path, pair, flag, permission, createParent, replication, blockSize, 0);
}

/**
 * To append data to file.
 * @param buf the data used to append.
 * @param size the data size.
 */
void OutputStream::append(const char * buf, int64_t size) {
    impl->append(buf, size);
}

/**
 * Flush all data in buffer and waiting for ack.
 * Will block until get all acks.
 */
void OutputStream::flush() {
    impl->flush();
}

/**
 * return the current file length.
 * @return current file length.
 */
int64_t OutputStream::tell() {
    return impl->tell();
}

/**
 * the same as flush right now.
 */
void OutputStream::sync() {
    impl->sync();
}

/**
 * close the stream.
 */
void OutputStream::close() {
    impl->close();
}

}
